package com.xxx.modules.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.modules.mapper.ProductMapper;
import com.xxx.modules.entity.Product;
import com.xxx.modules.service.ProductService;
import com.xxx.modules.mapper.UserMapper;
import com.xxx.modules.utils.Result;
import com.xxx.modules.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import com.xxx.modules.utils.TimeUtil;
/**
 * 车辆
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-03-30
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;



    /**
     *  获取所有车辆接口实现类
     * @param pageNum
     * @param pageSize
     * @param product
     * @return
     */
    @Override
    public Result selectProductList(Product product, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Product> data = productMapper.selectListInfo(product);
       return ResultUtil.success(1,"成功",new PageInfo<>(data));
    }


    /**
     * 获取单个车辆接口实现类
     * @param id
     * @return
     */
    @Override
    public Result selectProductInfo(Integer id) {
        Product product = productMapper.selectById(id);
        return ResultUtil.success(1,"成功",product);
    }

    /**
     * 保存车辆接口实现类
     * @param product
     * @return
     */
    @Override
    public Result saveProductInfo(Product product) {
        product.setStatus(1);
        QueryWrapper<Product> carNumberWrapper = new QueryWrapper<>();
        carNumberWrapper.eq("car_number",product.getCarNumber());
        Product carNumberInfo = productMapper.selectOne(carNumberWrapper);
        if (carNumberInfo != null){
            return ResultUtil.error(-1,"车牌号重复");
        }
        product.setCreateTime(TimeUtil.getCurrentTime());
        product.setUpdateTime(TimeUtil.getCurrentTime());
        productMapper.insert(product);
        return ResultUtil.success(1,"成功",null);
}

    /**
     * 更新车辆接口实现类
     * @param product
     * @return
     */
    @Override
    public Result updateProductInfo(Product product) {
        QueryWrapper<Product> carNumberWrapper = new QueryWrapper<>();
        carNumberWrapper.eq("car_number",product.getCarNumber()).ne("id",product.getId());
        Product carNumberInfo = productMapper.selectOne(carNumberWrapper);
        if (carNumberInfo != null){
            return ResultUtil.error(-1,"车牌号重复");
        }
        product.setUpdateTime(TimeUtil.getCurrentTime());
        productMapper.updateById(product);
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 根据id删除车辆删除接口实现类
     * @param id
     * @return
     */
    @Override
    public Result delProductInfo(Integer id) {
        productMapper.deleteById(id);
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 根据id集合批量删除车辆接口实现类
     * @param idList
     * @return
     */
    @Override
    public Result delBatchProductInfo(String idList) {
        List<String> list = Arrays.asList(idList.split(","));
        productMapper.deleteBatchIds(list);
        return ResultUtil.success(1,"成功",null);
    }



}